package cn.luoshaocheng.order.service.impl;

import cn.luoshaocheng.basic.util.PageList;
import cn.luoshaocheng.order.domain.OrderPetAcquisition;
import cn.luoshaocheng.order.dto.OrderDto;
import cn.luoshaocheng.order.mapper.OrderPetAcquisitionMapper;
import cn.luoshaocheng.order.query.OrderPetAcquisitionQuery;
import cn.luoshaocheng.order.service.IOrderPetAcquisitionService;
import cn.luoshaocheng.basic.service.impl.BaseServiceImpl;
import cn.luoshaocheng.org.domain.Employee;
import cn.luoshaocheng.org.domain.Shop;
import cn.luoshaocheng.org.mapper.EmployeeMapper;
import cn.luoshaocheng.org.mapper.ShopMapper;
import cn.luoshaocheng.pet.domain.Pet;
import cn.luoshaocheng.pet.mapper.PetMapper;
import cn.luoshaocheng.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, Long id) {
        Employee employee = employeeMapper.loadByLoginInfoId(id);//
        if(employee.getShop_id()!=null){
            Shop shop = shopMapper.loadById(employee.getShop_id());
            if(shop.getAdmin_id()!=employee.getId()){//查询出店长
                query.setEmpId(employee.getId());
            }
            query.setShopId(employee.getShop_id());//查询出店员
        }
        return super.queryPage(query);
    }

    @Override
    public void confirm(OrderDto dto) {
        BigDecimal money = dto.getMoney();
        Long oderId = dto.getId();
        Integer payType = dto.getPayType();
        /**
         * 1. 改订单状态，金额, 支付方式
         * 2. 改寻主消息状态
         * 3. 改pet金额
         */
        // 1.改订单状态，金额, 支付方式
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(oderId);
        orderPetAcquisition.setState(2);
        orderPetAcquisition.setPrice(money);
        orderPetAcquisition.setPaytype(payType);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 2.改寻主消息状态
        searchMasterMsgMapper.updateState(orderPetAcquisition.getSearchMasterMsgId());
        //3. 改pet金额
        Pet pet = petMapper.loadById(orderPetAcquisition.getPetId());
        pet.setCostprice(money);
        petMapper.update(pet);

    }

    @Override
    public void cancel(Long id) {
        /**
         * 1. 修改订单状态
         * 2. 寻主消息的shopid=null
         * 3. pet 删除
         */
        // 1. 修改订单状态
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 2. 寻主消息的shopid=null
        searchMasterMsgMapper.reject(orderPetAcquisition.getSearchMasterMsgId());
        // 3. pet 删除
        petMapper.remove(orderPetAcquisition.getPetId());

    }
}
